package com.hotau.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public abstract class DownloadFileAsyncTask extends GeneralAsynTask {

	public static final String SDCARD = "SDCARD";
	public static final String PRIVATE = "PRIVATE";

	public DownloadFileAsyncTask(Context context) {
		super(context);
	}

	public DownloadFileAsyncTask(Context context, String prompt) {
		super(context, prompt);
	}

	public DownloadFileAsyncTask(Context context, ProgressDialog progressDialog) {
		super(context, progressDialog);
	}

	protected void onProgressUpdate(String... progress) {
		Log.d("mytag", "ANDRO_ASYNC=" + progress[0]);
		progressDialog.setProgress(Integer.parseInt(progress[0]));
	}

	@Override
	public void doPreExecuteBeforeDialogShow() {
		progressDialog.setMessage("下载文件");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setCancelable(true);
	}

	@Override
	protected String doInBackground(String... params) {

		Log.d("mytag", "下载 url=" + params[0]);

		// 参数的第一个为url，第二个参数为File名称，第三个参数选填为写私有(PRIVATE)还是写sdcard(SDCARD)

		try {

			if (params.length < 2) {
				throw new IllegalArgumentException("参数个数不正确");
			}
			HttpClient client = new DefaultHttpClient();
			HttpGet post = new HttpGet(params[0]);
			HttpResponse response;

			response = client.execute(post);
			HttpEntity entity = response.getEntity();
			long length = entity.getContentLength();
			InputStream is = entity.getContent();
			FileOutputStream fileOutputStream = null;
			String writeType = PRIVATE;
			try {
				writeType = params[2];
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			Log.d("mytag", "url=" + params[0] + " fileName=" + params[1]
					+ " writeType=" + writeType);
			
			if (is != null) {
				File file;
				if (PRIVATE.equals(writeType)) {
					fileOutputStream = context.openFileOutput(params[1],
							Context.MODE_WORLD_READABLE);
				} else {
					fileOutputStream = new FileOutputStream(new File(
							Environment.getExternalStorageDirectory(),
							params[1]));
				}
				Log.d("mytag", "开始下载 length=" + length);

				byte[] buf = new byte[1024];
				int ch = -1;
				int count = 0;
				while ((ch = is.read(buf)) != -1) {
					fileOutputStream.write(buf, 0, ch);
					count += ch;
					publishProgress((int) ((count * 100) / length));
					if (length > 0) {
					}
				}
			}
			fileOutputStream.flush();
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}
			Log.d("mytag", "下载完成");

			if (is != null) {
				is.close();
			}
			fileOutputStream.flush();
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
			Log.e("mytag", "下载失败");
			// 更新失败
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("mytag", "下载失败");
		}
		return null;
	}
}
